package controller;

import com.jfinal.aop.Before;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import interceptor.LoginCheck;
import model.Topic;

import java.util.List;


public class LoginController extends Controller {
@ActionKey("/login")
   public void login(){
    System.out.println("/login");
      render("../login.html");
    }
@ActionKey("/checklogin")
    public void checklogin(){

        String username=getPara("username");
        String password = getPara("pass");
        List<Record> user = Db.find("select * from user where username=?",username );

        if(user.get(0).get("username").toString().equals(username)&&user.get(0).get("password").toString().equals(password))
        {
            setSessionAttr("user",user);

            setSessionAttr("username",username);
            setSessionAttr("userid",user.get(0).get("id"));
            setSessionAttr("nickname",user.get(0).get("nickname"));
            setAttr("nickname",user.get(0).get("nickname"));
          //  List<Record> topics = Db.find("select * from topic limit 20");
            Page<Topic> page = Topic.dao.paginate(1,15,"SELECT topic.id,topic.user_id,topic.category_id,topic.`name`,topic.content,topic.pub_date,topic.deleted,category.`name` as categoryname ",
                    "FROM topic ,category WHERE topic.category_id=category.id");

            setAttr("page",page);
            List<Record> category = Db.find("select * from category");
            setAttr("category",category);
            if(user.get(0).get("admin"))
            {

                redirect("/showCategory");

            }else
            renderFreeMarker("/template/topic.ftl");
        }
        else renderHtml("用户名或密码错误");
    }
    @ActionKey("/getpage")
    public void getpage(){
       int  pagenum = getParaToInt("pagenum",1);
        Page<Topic> page = Topic.dao.paginate(pagenum,15,"SELECT topic.id,topic.user_id,topic.category_id,topic.`name`,topic.content,topic.pub_date,topic.deleted,category.`name` as categoryname ",
                "FROM topic ,category WHERE topic.category_id=category.id");

        setAttr("page",page);
        List<Record> category = Db.find("select * from category ");
        setAttr("category",category);
        renderFreeMarker("/template/topic.ftl");
    }
    @ActionKey("/getTopic")
    public void gettopic(){
        String name = getPara("name");
        Page<Topic> page = Topic.dao.paginate(1,15,"SELECT topic.id,topic.user_id,topic.category_id,topic.`name`,topic.content,topic.pub_date,topic.deleted,category.`name` as categoryname ",
                "FROM topic ,category WHERE topic.category_id=category.id and category.name='"+name+"'");

        setAttr("page",page);
        List<Record> category = Db.find("select * from category where deleted =0");
        setAttr("category",category);
        renderFreeMarker("/template/topic.ftl");
    }
   @Before(LoginCheck.class)
    @ActionKey("/mine")
    public void mine(){
       Page<Topic> page = Topic.dao.paginate(1,15,"SELECT topic.id,topic.user_id,topic.category_id,topic.`name`,topic.content,topic.pub_date,topic.deleted,category.`name` as categoryname ",
               "FROM topic ,category WHERE topic.category_id=category.id and topic.user_id='"+getSessionAttr("userid")+"'");

       setAttr("page",page);
       setAttr("userid",getSessionAttr("userid"));
       List<Record> category = Db.find("select * from category where deleted =0");
       setAttr("category",category);
       renderFreeMarker("/template/mine.ftl");
    }
    @ActionKey("/pubTopic")
    public void pubTopic(){
        String name = getPara("name");
        String content = getPara("content");
        Integer category_id = getParaToInt("category_id");
        Db.update("insert into topic values(null,'"+getSessionAttr("userid")+"',"+category_id+",'"+name+"','"+content+"',now(),0)");
        redirect("/mine");
    }
    @Before(LoginCheck.class)
    @ActionKey("/modifyuser")
    public void moduser(){
        String email = getPara("email");
        String nickname = getPara("nickname");

        if(email!=null&&nickname!=null)
        {
            Db.update("UPDATE user set nickname='"+nickname+"',email='"+email+"' where id ="+getSessionAttr("userid"));
            //更新session
            setSessionAttr("user", Db.find("select * from user where id="+getSessionAttr("userid") ));
            redirect("/userinfo");
        }
        else
            renderHtml("非法输入！");
    }
    @Before(LoginCheck.class)
    @ActionKey("/userinfo")
    public void  userinfo(){
        List<Record> category = Db.find("select * from category where deleted =0");
        setAttr("category",category);
        setAttr("userinfo",getSessionAttr("user"));
        System.out.println(getSessionAttr("user").toString());
        renderFreeMarker("/template/userinfo.ftl");
    }

}
